<template>
  <a-modal
    :visible="visible"
    title="请选择您要添加的项目类型"
    @cancel="handleCancel"
    :footer="null"
    centered
    width="70%"
  >
    <a-row :gutter="[12, 12]">
      <a-col
        :span="6"
        v-for="(type, index) of typeList"
        :key="index"
        @mouseenter="hoverIndex = index"
        @mouseleave="hoverIndex = -1"
        @click="handleTypeClick(type.alias)"
      >
        <div class="col">
          <img
            :src="type.bg_img_txt"
            class="bg-img"
            :class="{
              'hover-class': hoverIndex === index,
            }"
          />
          <div class="box">
            <img :src="type.icon_img_txt" class="icon-img" />
            <div class="name">{{ type.name }}</div>
          </div>
        </div>
      </a-col>
    </a-row>
  </a-modal>
</template>

<script>
import { defineComponent, reactive, toRefs, watch } from "vue";
import { useRouter } from "vue-router";
import request from "../../../../common/utils/request";

export default defineComponent({
  name: "ChooseVillageType",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  setup(props, { emit }) {
    const $router = useRouter();
    const handleTypeClick = (alias) => {
      handleCancel();
      $router.push({ name: "projectEdit", query: { type: alias } });
    };

    const handleCancel = () => {
      emit("close");
    };

    const state = reactive({
      typeList: [],
      hoverIndex: -1,
    });

    const getVillageTypeList = () => {
      if (state.typeList.length) {
        return;
      }
      request.get("/village", "/village/type/list").then((res) => {
        state.typeList = res.list;
      });
    };

    watch(
      () => props.visible,
      (val) => {
        if (val) {
          getVillageTypeList();
        }
      }
    );

    return {
      ...toRefs(state),
      handleCancel,
      handleTypeClick,
    };
  },
});
</script>

<style lang="less" scoped>
.absolute {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}
.col {
  position: relative;
  height: 0;
  width: 100%;
  padding-bottom: 56.25%;
  overflow: hidden;
  cursor: pointer;

  .bg-img {
    &:extend(.absolute);
    z-index: 1;
    transition: all 0.3s;
  }
  .hover-class {
    transform: scale(1.2);
  }
  .box {
    &:extend(.absolute);
    z-index: 2;
    background: rgba(0, 0, 0, 0.4);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #fff;
    .icon-img {
      width: 18%;
      height: auto;
    }
    .name {
      font-size: 15px;
      font-weight: bold;
      margin-top: 10px;
    }
  }
}
</style>
